package com.easily.factory.nfactoryTest;

import com.easily.factory.Pool;
import com.easily.system.exception.CarrotException;

import java.lang.annotation.Annotation;
import java.util.Map;

public class ObjectBean {
    // 唯一标识
    private String id;
    // 可能是类全名，也可能是接口名
    private String name;
    // 判断是否是接口下的bean
    private boolean isInterfaceBean = false;
    // Class.forName(className) 获得
    private Class<?> clazz;
    // class.newInstance() 获得
    private Object object;
    // object 代理后的对象
    private Object proxyObject = null;
    // 传入的属性值
    private Map<String,Object> paramsMap;
    // 类头上的注解class
    private Class<? extends Pool> labelClass;
    // bean功能性代理字段，可能是注解的value，也可能是根据bean解析后生成的正则等
    private String beanProxyValue;

    public void newInstance(){
        try {
            this.object = this.clazz.newInstance();
        }catch (InstantiationException | IllegalAccessException e){
            throw new CarrotException("bean 实例化失败：" + e.getMessage());
        }
    }

    public <T> T getBean(Class<T> clazz){
        return proxyObject == null ? clazz.cast(object):clazz.cast(proxyObject);
    }

    public Object getBean(){
        return proxyObject == null ? object:proxyObject;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isInterfaceBean() {
        return isInterfaceBean;
    }

    public void setInterfaceBean(boolean interfaceBean) {
        isInterfaceBean = interfaceBean;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public Object getProxyObject() {
        return proxyObject;
    }

    public void setProxyObject(Object proxyObject) {
        this.proxyObject = proxyObject;
    }

    public Map<String, Object> getParams() {
        return paramsMap;
    }

    public Object putParam(String paramName,Object param){
        return paramsMap.put(paramName,param);
    }

    public void setParams(Map<String, Object> paramsMap) {
        this.paramsMap = paramsMap;
    }

    public Class<? extends Pool> getLabelClass() {
        return labelClass;
    }

    public void setLabelClass(Class<? extends Pool> labelClass) {
        this.labelClass = labelClass;
    }

    public String getBeanProxyValue() {
        return beanProxyValue;
    }

    public void setBeanProxyValue(String beanProxyValue) {
        this.beanProxyValue = beanProxyValue;
    }
}
